#!/usr/bin/perl -w

#####################################################################
# Program Variables
#####################################################################
use FindBin qw($Bin);
use lib "$Bin";
use config;

$lockfile = "$graphdatafile.lock";

#####################################################################
# Program Modules
#####################################################################
# Database Modules
use DBI;

#####################################################################
# Program Start
#####################################################################

if (-f "$lockfile") {
	exit 100;
} else {
	`touch "$lockfile"`;
}

my $dbh = DBI->connect("dbi:SQLite:dbname=$database_file", "", "");

#$dbh->{sqlite_see_if_its_a_number} = 1;

open(OUTPUT, ">$graphdatafile");

# Table Headers
my @tableheaders = ("Collected");
my $test_sth     = $dbh->prepare("SELECT * FROM tests ORDER BY displayorder ASC");
$test_sth->execute();
while (my $testref = $test_sth->fetchrow_hashref()) {
    my %testhash = %{$testref};
    push(@tableheaders, $testhash{name});
}

print OUTPUT "# Columns are listed by the display order in the database.\n";
print OUTPUT "# " . join("\t", @tableheaders) . "\n";
if ($debug == 1) {
    print "!!!! Debugging turned on.  Do not use this way in production !!!!\n";
    print "# Columns are listed by the display order in the database.\n";
    print "# " . join("\t", @tableheaders) . "\n";
}

# Get collection time list
my @collected_times = @{ $dbh->selectcol_arrayref("SELECT DISTINCT collected FROM aquarium ORDER BY collected DESC") };
my $data_sth        = $dbh->prepare("SELECT collected,name,value FROM aquarium WHERE collected=? AND name=?");

#my $test_sth		= $dbh->prepare_cached("SELECT * FROM tests ORDER BY displayorder ASC");

foreach $time (@collected_times) {
    print OUTPUT "$time\t";
    if ($debug == 1) {
        print "$time\t";
    }

    $test_sth->execute();
    while (my $testref = $test_sth->fetchrow_hashref()) {
        my %testhash = %{$testref};
        my $name     = $testhash{name};

        if ($debug == 1) {
            print "$name:";
        }

        $data_sth->execute($time, $name);
        my $data_ref = $data_sth->fetchrow_hashref();
        if (defined($data_ref)) {
            my %data_hash = %{$data_ref};
            if ((defined($data_hash{value})) and ($data_hash{value} ne "")) {
                print OUTPUT "$data_hash{value}\t";
                if ($debug == 1) {
                    print "$data_hash{value}\t";
                }
            } else {
                print OUTPUT "NaN\t";
                if ($debug == 1) {
                    print "NaN\t";
                }
            }
        } else {
            print OUTPUT "NaN\t";
            if ($debug == 1) {
                print "NaN\t";
            }
        }
    }

    print OUTPUT "\n";
    if ($debug == 1) {
        print "\n";
    }
}

$data_sth->finish();
$dbh->disconnect();
close(OUTPUT);

unlink "$lockfile";

exit 0;

